$(function () {


    $("#jqGrid").jqGrid({
        url: baseURL + 'jysjxs/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: "id", width: 45, key: true,hidden:true },
			{ label: '试卷名称', name: 'sjName', index: 'sj_name', width: 80 },
			{ label: '学号', name: 'xsId', index: 'xs_id', width: 80 },
            { label: '姓名', name: 'candidate', index: 'nick_name', width: 80 },
            { label: '得分', name: 'sjScore', index: 'sj_score', width: 80 },
			{ label: '答题时间', name: 'dtDate', index: 'dt_date', width: 80 }, 
			{ label: '考试备注', name: 'dtContent', index: 'dt_content', width: 80 }, 
			{ label: '评卷人', name: 'pjr', index: 'pjr', width: 80 }, 
			{ label: '考试时长', name: 'sc', index: 'sc', width: 80 }, 
			{ label: '状态', name: 'state', index: 'state', width: 80 ,formatter: function(value, options, row){
                var str="";
                for(var i in vm.status){
                    if(vm.status[i].value===value){
                        str=vm.status[i].text;
                    }
                }
                return  str;
            } }
        ],
		viewrecords: true,
        height: 365,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{

        },
		showList: true,
		title: null,
		jySjXs: {},
        status:[{text:'请选择',value:''},{text:'未考完',value:'0'},{text:'已考完',value:'1'}],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jySjXs = {};
		},
		update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.jySjXs.id == null ? "jysjxs/save" : "jysjxs/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.jySjXs),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "jysjxs/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "jysjxs/info/"+id, function(r){
                vm.jySjXs = r.jySjXs;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'name':vm.q.name}
            }).trigger("reloadGrid");
		}
	}
});